/*
 * DocTiengViet.cpp
 *
 *  Created on: May 9, 2011
 *      Author: MongKieu
 */

#include "../inc/Utility/VietnameseReader.h"
#include "../inc/Utility/MyParserTiengViet.h"

using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Graphics;
using namespace Osp::Media;
using namespace Osp::Io;

VietnameseReader::VietnameseReader() {
	index = 0;
	pPlayer = null;
	listWord = new ArrayList();
	listWord->Construct();
}

VietnameseReader::~VietnameseReader() {
	if (pPlayer) {
		PlayerState nowState = pPlayer->GetState();
		if (nowState == PLAYER_STATE_PLAYING || nowState == PLAYER_STATE_PAUSED) {
			pPlayer->Stop();
			pPlayer->Close();
			AppLog("audio player closed");
		} else if (nowState == PLAYER_STATE_OPENED || nowState
				== PLAYER_STATE_ENDOFCLIP || nowState == PLAYER_STATE_STOPPED) {
			pPlayer->Close();
			AppLog("audio player closed");
		}
		delete pPlayer;
		pPlayer = null;
	}
}

void VietnameseReader::OnPlayerOpened(result r) {
	r = pPlayer->Play();
	if (IsFailed(r)) {
		AppLog(">>>>>> (VideoPlayer::VideoPlayerPlay) Play has failed: %s\n",
				GetErrorMessage(r));
		return;
	}
}
void VietnameseReader::OnPlayerEndOfClip(void) {
	pPlayer->Close();

	if (index < listWord->GetCount()) {
		createPathOfFile();
	}

}
void VietnameseReader::OnPlayerBuffering(int percent) {
	AppLog("Vao play buffer");
}
void VietnameseReader::OnPlayerErrorOccurred(PlayerErrorReason r) {
	AppLog("Loi xay ra khi doc");
}
void VietnameseReader::OnPlayerInterrupted() {
	AppLog("Vao play interupted");
}
void VietnameseReader::OnPlayerReleased() {
	AppLog("Vao player release");
}
void VietnameseReader::OnPlayerSeekCompleted(result r) {
	AppLog("Vao player seek");
}

result VietnameseReader::talk() {
	result r = E_SUCCESS;
	pPlayer = new Player();
	if (!pPlayer) {
		AppLog(">>>>>> pPlyaer = new Player() has failed\n");
		return E_FAILURE;
	}
	pPlayer->Construct(*this, &bufferInfoForVideo);
	pPlayer->SetVolume(100);
	r = createPathOfFile();
	return r;
}

result VietnameseReader::readText(String text) {
	AppLog("ReadText:%S", text.GetPointer());
	result r = E_SUCCESS;
	MyParserTiengViet *pMyParserTiengViet = new MyParserTiengViet();
	String ketqua = pMyParserTiengViet->chuyenSangTelex(text);
	ArrayList *kqList = new ArrayList();
	kqList = pMyParserTiengViet->danhSachCacWordTrongCau(ketqua);

	for (int i = 0; i < kqList->GetCount(); i++) {
		String *wordPtr = static_cast<String*> (kqList->GetAt(i));
		listWord->Add(*(new String(wordPtr->GetPointer())));
	}

	return r;
}
result VietnameseReader::createPathOfFile() {
	result r = E_SUCCESS;
	if (index < listWord->GetCount()) {
		AppLog("Index:%d", index);
		String url;
		bool ketqua;
		String text;
		String *fileName1;
		do {
			if (index >= listWord->GetCount()) {
				return r;
			}
			url = "";
			fileName1 = static_cast<String*> (listWord->GetAt(index));
			text = fileName1->GetPointer();
			url.Append("/Home/MapData/Voices/Vietnamese/");
			mchar syllableFirst;
			text.GetCharAt(0, syllableFirst);
			url.Append(syllableFirst);
			url.Append("/");
			url.Append(text);
			url.Append(".wav");
			AppLog("Khong co Url :%S", url.GetPointer());
			ketqua = File::IsFileExist(url);
			index++;

		} while (ketqua == false);
		if (ketqua == true) {
			AppLog("File ton tai");
			r = pPlayer->OpenFile(url, true);
			if (IsFailed(r)) {
				AppLog(">>>>>> (AudioPlayerOpen) pPlayerW2->OpenFile has failed: %s\n",
						GetErrorMessage(r));
				r = E_FAILURE;
			}
			AppLog("url:%S", url.GetPointer());
		} else {
			AppLog("Khong co url nay");
		}
	}
	return r;
}

result VietnameseReader::readText() {

	listWord->RemoveAll(true);
	index=0;
	for (int i = 0; i < (int) arrText.size(); i++) {
		readText(arrText[i]);
	}
	//readText(arrText[0]);
	//r = talk();
	AppLog("Chieu dai chuoi:%d", listWord->GetCount());
	talk();

	return E_SUCCESS;
}
